<?php
function upload($file, $file_path)
{
	$servername = "localhost";     //数据库服务器主机名
	$username = "root";           //数据库服务器用户名
	$password = "root";           //数据库服务器密码
	$database = "news";           //数据库名

	// 创建连接
	global $conn;
	$conn = @mysqli_connect($servername, $username, $password) or die("连接失败: " . mysqli_connect_error());;
	
	mysqli_select_db($conn, $database) or die("选择数据库失败: " . mysqli_error($conn));
	$sql = "SELECT news_id FROM news WHERE news_attachment='" . $file['name'] . "'";
	$result = mysqli_query($conn, $sql);
	if (mysqli_num_rows($result) > 0) {
		return "目录中存在同名文件，上传失败，请修改需上传附件的名称再重新上传！";
	}
	$error = $file['error'];
	switch ($error) {
		case 0:
			$file_name = $file['name'];
			$file_temp = $file['tmp_name'];
			$destination = $file_path . "/" . $file_name;
			move_uploaded_file($file_temp, $destination);
			return "文件上传成功！";
		case 1:
			return "上传附件超过了php.ini中upload_max_filesize选项限制的值！";
		case 2:
			return "上传附件的大小超过了form表单MAX_FILE_SIZE选项指定的值！";
		case 3:
			return "附件只有部分被上传！";
		case 4:
			return "没有选择上传附件！";
	}
}
function extension_name($file_name)
{
	$extension = explode(".", $file_name);
	$key = count($extension) - 1;
	return $extension[$key];
}
function content_type($extension)
{
	$mime_types = array(
		'txt' => 'text/plain',
		'htm' => 'text/html',
		'html' => 'text/html',
		'php' => 'text/html',
		'css' => 'text/css',
		'js' => 'application/javascript',
		'xml' => 'application/xml',
		'swf' => 'application/x-shockwave-flash',
		'flv' => 'video/x-flv',
		// images 
		'png' => 'image/png',
		'jpe' => 'image/jpeg',
		'jpeg' => 'image/jpeg',
		'jpg' => 'image/jpeg',
		'gif' => 'image/gif',
		'bmp' => 'image/bmp',
		'ico' => 'image/vnd.microsoft.icon',
		// archives 
		'zip' => 'application/zip',
		'rar' => 'application/x-rar-compressed',
		'exe' => 'application/x-msdownload',
		// audio/video 
		'mp3' => 'audio/mpeg',
		'qt' => 'video/quicktime',
		'mov' => 'video/quicktime',
		// adobe 
		'pdf' => 'application/pdf',
		// ms office 
		'doc' => 'application/msword',
		'rtf' => 'application/rtf',
		'xls' => 'application/vnd.ms-excel',
		'ppt' => 'application/vnd.ms-powerpoint'
	);
	if (array_key_exists($extension, $mime_types)) {
		return $mime_types["$extension"];
	} else {
		return "application/octet-stream";
	}
}
function download($file_dir, $file_name)
{
	if (!file_exists($file_dir . $file_name)) { //检查文件是否存在  
		exit("文件不存在或已删除");
	} else {
		$file = fopen($file_dir . $file_name, "r"); // 打开文件  
		//取得文件的扩展名 
		$extension_name = extension_name($file_name);
		//根据扩展名取得文件的MIME类型 
		$content_type = content_type($extension_name);
		//设置浏览器打开正文信息的打开方式 
		header("Content-Type:$content_type");
		//强迫浏览器显示保存对话框，并提供一个推荐的文件名 
		header("Content-Disposition: attachment; filename=" . $file_name);
		// 输出文件内容 
		echo fread($file, filesize($file_dir . $file_name));
		fclose($file);
		exit;
	}
}
function delete($file_dir, $file_name)
{
	if (!file_exists($file_dir . $file_name)) { //检查文件是否存在  
		exit("文件不存在或已删除");
	} else {
		if (file_exists($file_dir)) {
			$res = unlink($file_dir . $file_name);
			if ($res) {
				echo "<script>alert('删除成功！');window.location.href = document.referrer;</script>";
				return "删除文件成功！";
			} else {
				echo "<script>alert('删除失败！');window.location.href = document.referrer;</script>";
				return "删除文件失败！";
			}
		} else {
			echo "<script>alert('删除文件失败！目录不存在！');window.location.href = document.referrer;</script>";
			return "删除文件失败！目录不存在！";
		}
	}
}
?>
